package com.controller
{
	import com.events.LoginEvent;
	import com.model.valueObjects.VOLoggedIn;
	import com.model.valueObjects.VoUser;
	import com.service.LoginService;
	import com.service.SubTablesDetails;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class LoginController extends AbstractController
	{
		[Inject]
		public var loginService:LoginService;
		[Inject]
		public var subTable:SubTablesDetails;
		[PostConstruct]
		public function initalizeLogin():void
		{
		model.currentScreen=0;
		}
		[EventHandler(event="LoginEvent.LOGGEDIN",properties="user")]
		public function getUser(user:VoUser):void
		{
			sh.executeServiceCall(loginService.getLogin(user),getLoginResult, handleFault );	
		}
		private function getLoginResult(event:ResultEvent):void
		{
		//	Alert.show("res  "+event.result);
			
		CursorManager.removeBusyCursor();
		var result:VOLoggedIn= event.result as VOLoggedIn;
		model.currentUser=result.firstname+" "+result.lastname;
		model.currentScreen=result.userIndex;
		model.clientId=result.client_id;
	//	model.currentDatabase='vvinn_'+String(result.dbname);
		model.currentDatabase=String(result.dbname);
		if(result.userIndex){
		model.loginText="Login";
		}else{
			model.loginText="Please Enter Correct Usrname or Passoword";
		}
		}
		[EventHandler(event="LoginEvent.LOGGEDOUT")]
		public function logout():void
		{
			sh.executeServiceCall(loginService.logout(),getLogOutResult, handleFault );	
		}	
		private function getLogOutResult(event:ResultEvent):void
		{
		}
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.message, "Error" );
		}
		
	}
}